package com.sjq.common.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.sjq.common.dao.GenericDao;
import com.sjq.common.dao.OperationLogDao;
import com.sjq.common.entity.OperationLog;
import com.sjq.common.util.PrimaryKeyUtil;


@Repository
public class OperationLogImpl extends GenericDao<OperationLog> implements OperationLogDao {
	
	private String getSelectSql(){
		String sql=" select  XXZJBH ,"+
							"CZDX_MC,"+
							"CAZR_GMSFHM,"+
							"CAZR_XM,"+
							"CAZR_IPDZ,"+
							"CAZR_DWDM,"+
							"CAZR_DWMC,"+
							"to_char(CAZSJ_RQSJ,'yyyy-mm-dd') CAZSJ_RQSJ ,"+
							"CZURL,"+
							"CZCS ," +
							"rownum row_num "+
					" from T_OPERATE_LOG t ";
		return sql;

	}
	
	private String getWheres(OperationLog czrz){
		String wheres=" where 1=1 ";
		if(czrz.getXxzjbh()!=null&&!"".equals(czrz.getXxzjbh())){
			wheres+=" and xxzjbh='"+czrz.getXxzjbh()+"' ";
		}
		if(czrz.getCazr_gmsfhm()!=null&&!"".equals(czrz.getCazr_gmsfhm())){
			wheres+=" and cazr_gmsfhm ='"+czrz.getCazr_gmsfhm()+"' ";
		}
		if(czrz.getCazr_dwdm()!=null&&!"".equals(czrz.getCazr_dwdm())){
			wheres+=" and cazr_dwdm ='"+czrz.getCazr_dwdm()+"' ";
		}
		if(czrz.getCazr_ipdz()!=null&&!"".equals(czrz.getCazr_ipdz())){
			wheres+=" and cazr_ipdz ='"+czrz.getCazr_ipdz()+"' ";
		}
		return wheres;
	}

	@Override
	public List<OperationLog> queryByCzrz(OperationLog czrz) {
		String sql=getSelectSql()+getWheres(czrz);
		
		return this.getJdbcTemplate().query(sql, new BeanPropertyRowMapper(OperationLog.class));
	}

	@Override
	public List<OperationLog> queryByPaging(OperationLog czrz, int start, int end) {
		String sql=" select * " +
					"	from ( "+getSelectSql()+
									getWheres(czrz)+" ) b "+
					"		where row_num between "+start+
					"					  and "+end;
		return this.getJdbcTemplate().query(sql, new BeanPropertyRowMapper(OperationLog.class));
	}

	@Override
	public int save(OperationLog czrz) {
		String sql=" insert into T_OPERATE_LOG ( XXZJBH,"+
													"CZDX_MC,"+
													"CAZR_GMSFHM,"+
													"CAZR_XM,"+
													"CAZR_IPDZ,"+
													"CAZR_DWDM,"+
													"CAZR_DWMC,"+
													"CAZSJ_RQSJ,"+
													"CZURL,"+
													"CZCS )"+
												"values( '"+PrimaryKeyUtil.getUUID()+"',"+
														"'"+czrz.getCzdx_mc()+"',"+
														"'"+czrz.getCazr_gmsfhm()+"',"+
														"'"+czrz.getCazr_xm()+"',"+
														"'"+czrz.getCazr_ipdz()+"',"+
														"'"+czrz.getCazr_dwdm()+"',"+
														"'"+czrz.getCazr_dwmc()+"',"+
														"sysdate,"+
														"'"+czrz.getCzurl()+"',"+
														"'"+czrz.getCzcs()+"')";
		return this.getJdbcTemplate().update(sql);
	}

	
	

}
